package com.openstack.javasdk;

import com.alibaba.fastjson.JSONObject;
import com.sun.xml.internal.bind.v2.runtime.unmarshaller.LocatorEx;
import org.openstack4j.api.Builders;
import org.openstack4j.api.OSClient.OSClientV3;
import org.openstack4j.model.common.ActionResponse;
import org.openstack4j.model.common.Payload;
import org.openstack4j.model.common.Payloads;
import org.openstack4j.model.image.v2.ContainerFormat;
import org.openstack4j.model.image.v2.DiskFormat;
import org.openstack4j.model.image.v2.Image;
import org.openstack4j.model.storage.block.Volume;

import org.openstack4j.model.storage.block.VolumeSnapshot;
import org.openstack4j.model.storage.block.VolumeType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.List;

@RestController
@RequestMapping(value = "/cinder")
public class CinderModule {

    @GetMapping("/volume/typeList")
    public String imageTypeList() {
        OSClientV3 os = Auth.getOS();
        List<? extends VolumeType> types = os.blockStorage().volumes().listVolumeTypes();
        return JSONObject.toJSONString(types);
    }

    @GetMapping("/volume/list")
    public String imageList() {
        OSClientV3 os = Auth.getOS();
        List<? extends Volume> volumes = os.blockStorage().volumes().list();
        return JSONObject.toJSONString(volumes);
    }

    @GetMapping("/volume/query")
    public String imageQuery(@RequestParam(value = "volumeId") String volumeId) {
        OSClientV3 os = Auth.getOS();
        Volume volume = os.blockStorage().volumes().get(volumeId);
        return JSONObject.toJSONString(volume);
    }

    @GetMapping("/volume/create")
    public String imageCreate() {
        OSClientV3 os = Auth.getOS();
        Volume v = os.blockStorage().volumes()
                .create(Builders.volume()
                        .name("Backup Volume")
                        .description("Simple volume to store backups on")
                        .size(20)
                        .build()
                );
        return JSONObject.toJSONString(v);
    }

    @GetMapping("/volume/createByImage")
    public String imageCreateByImage() {
        OSClientV3 os = Auth.getOS();
        Volume v = os.blockStorage().volumes()
                .create(Builders.volume()
                        .name("Ubuntu 12.04 LTS")
                        .description("Bootable install volume")
                        .imageRef("6f0310c3-f333-4828-9265-82118314df79")
                        .size(20)
                        .bootable(true)
                        .build()
                );
        return JSONObject.toJSONString(v);
    }

    @GetMapping("/volume/update")
    public String imageUpdate(@RequestParam(value = "volumeId") String volumeId) {
        OSClientV3 os = Auth.getOS();
        ActionResponse response = os.blockStorage().volumes().update(volumeId, "New Name", "New Description");
        return JSONObject.toJSONString(response);
    }

    @GetMapping("/volume/delete")
    public String imageDelete(@RequestParam(value = "volumeId") String volumeId) {
        OSClientV3 os = Auth.getOS();
        ActionResponse response = os.blockStorage().volumes().delete(volumeId);
        return JSONObject.toJSONString(response);
    }

    @GetMapping("/snapshot/list")
    public String snapshotList() {
        OSClientV3 os = Auth.getOS();
        List<? extends VolumeSnapshot> snapshots = os.blockStorage().snapshots().list();
        return JSONObject.toJSONString(snapshots);
    }

    @GetMapping("/snapshot/query")
    public String snapshotQuery(@RequestParam( value = "snapshotId") String snapshotId) {
        OSClientV3 os = Auth.getOS();
        VolumeSnapshot snapshot = os.blockStorage().snapshots().get(snapshotId);
        return JSONObject.toJSONString(snapshot);
    }

//    Invalid volume: Volume abbe430f-a591-4af0-8b81-b277913edfa8 status must be available, but current status is: in-use.
    @GetMapping("/snapshot/create")
    public String snapshotCreate() {
        OSClientV3 os = Auth.getOS();
        VolumeSnapshot snapshot = os.blockStorage().snapshots()
                .create(Builders.volumeSnapshot()
                        .name("Snapshot 2014-01-01 - Before Upgrade")
                        .volume("abbe430f-a591-4af0-8b81-b277913edfa8")
                        .build()
                );
        return JSONObject.toJSONString(snapshot);
    }
}

